package hirt;

import java.nio.ByteBuffer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;



public class Message {
	public byte type;
	public byte length;
	public Action content;

        public static final byte CHAT_MESSAGE = 1;
        public static final byte MOVE_MESSAGE = 2;
        public static final byte CREATE_MESSAGE = 3;
        public static final byte TRANSFER_MESSAGE = 4;
        public static final byte PLAYERCHANGE_MESSAGE = 5;
        public static final byte LOCALPLAYERCREATE_MESSAGE = 6;
        public static final byte REMOVE_MESSAGE = 7;
        public static final byte REVIEW_MODE_MESSAGE = 8;
        public static final byte REFRESH_TIME_MESSAGE = 9;
        public static final byte REQUEST_CREATE_MESSAGE = 10;
        public static final byte PLAY_MODE_MESSAGE = 11;
        public static final byte EDITOR_MODE_MESSAGE = 12;
        public static final byte SCENARIO_DATA_MESSAGE = 13;
        public static final byte CLEAR_SCENARIO_MESSAGE = 14;
        public static final byte LOCALPLAYERASSIGN_MESSAGE = 15;
        public static final byte SAVE_SCENARIO_MESSAGE = 16;
        public static final byte LOAD_SCENARIO_MESSAGE = 17;
        public static final byte CAR_CONDITION_MESSAGE = 18;
        public static final byte SET_LANE_ADJACENCY_MESSAGE = 19;
        public static final byte SET_CONE_AMOUNT_MESSAGE = 20;



	
	public Message(byte Type, Action Message)
	{
		type = Type;
		content = Message;
                length = (byte) content.toByte().limit();
	}

        public Message(byte Type)
        {
            type = Type;
            content = new Action();
            length = -1;
        }

        public Message(Action action)
        {
            content = action;
            type = content.messageType;
            length = (byte) content.toByte().limit();
        }
	
	public Message()
	{
		type = -1;
		content = new Action();
		length = 0;
	}
	
	public Message(ByteBuffer message, Vector<Item> items)
	{
		type = message.get(0);
		length = message.get(1);
                if (length >=0)
                {
                    ByteBuffer buf = null;
                    try {
                        buf = ByteChanger.wrap(message, 2, length);
                        //buf.wrap(message.array(), 2, length);
                        /*Item owner;
                        if (message[2] == 0)
                        owner = null;
                        else
                        owner = new Item();*/
                    } catch (Exception ex) {
                        System.out.println("Error creating Message:  " + ex.getMessage());
                    }
                    //buf.wrap(message.array(), 2, length);
                    /*Item owner;
                    if (message[2] == 0)
                        owner = null;
                    else
                        owner = new Item();*/
                    switch (type)
                    {
                        case CHAT_MESSAGE:
                            content = new CommunicationAction(buf, items);
                            break;
                        case MOVE_MESSAGE:
                            content = new MovementAction(buf, items);
                            break;
                        case CREATE_MESSAGE:
                        case LOCALPLAYERCREATE_MESSAGE:
                            content = new CreationAction(buf, items);
                            break;
                        case TRANSFER_MESSAGE:
                            content = new TransferAction(buf, items);
                            break;
                        case PLAYERCHANGE_MESSAGE:
                            content = new PlayerChangeAction(buf, items);
                            break;
                        case REMOVE_MESSAGE:
                            content = new RemoveAction(buf, items);
                            break;
                        case REFRESH_TIME_MESSAGE:
                            content = new Action(buf, items);
                            break;
                        case REQUEST_CREATE_MESSAGE:
                            content = new RequestCreateAction(buf,items);
                            break;
                        case SCENARIO_DATA_MESSAGE:
                        case SAVE_SCENARIO_MESSAGE:
                        case LOAD_SCENARIO_MESSAGE:
                            content = new ScenarioDataAction(buf, items);
                            break;
                        case LOCALPLAYERASSIGN_MESSAGE:
                            content = new Action(buf, items);
                            break;
                        case CAR_CONDITION_MESSAGE:
                            content = new CarConditionAction(buf, items);
                            break;
                        case SET_LANE_ADJACENCY_MESSAGE:
                            content = new TransferAction(buf, items);
                        case SET_CONE_AMOUNT_MESSAGE:
                            content = new RequestCreateAction(buf,items);
                    }
                }

	}
	
	public ByteBuffer toByte()
	{
		ByteBuffer buf = ByteBuffer.allocate(120);
		//byte[] tmp = new byte[2];
		buf.put(type);
		buf.put(length);
                if (length >= 0)
                {
                    ByteBuffer tmp = content.toByte();
                    tmp.position(0);
                    buf.put( tmp.array() );
                }
		return buf;
	}
	
	private byte[] append(byte[] bit1,byte[] bit2)
	{
		byte[] buf = new byte[bit1.length+bit2.length];
		int i;
		for(i=0; i<bit1.length;i++)
		{
			buf[i] = bit1[i];
		}
		for(int j = 0; j < bit2.length; j++)
		{
			buf[j+i] = bit2[j];
		}
		return buf;
	}



}
